package com.example.yuejian11.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.example.yuejian11.R;
import com.example.yuejian11.bean.ActivityDetailEntity;
import com.example.yuejian11.common.Url;
import com.example.yuejian11.view.TitleView;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;

import butterknife.BindView;
import butterknife.OnClick;


public class RecentDetail extends BaseActivity {


    private Intent intent;
    private String mid;
    @BindView(R.id.name)
    TextView name;
    @BindView(R.id.address)
    TextView address;
    @BindView(R.id.intro)
    TextView intro;

    @BindView(R.id.time)
    TextView time;
    @BindView(R.id.img)
    ImageView imageView;
    private Context context;

    @BindView(R.id.title_activity)
    TitleView title_activity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_recent_game);
        super.onCreate(savedInstanceState);

        context = this;
        //设置视图
        initView();
        //设置数据
        initDate();
    }
    private String uid;    Integer isCollect;
    private void initDate() {
        //
        intent = getIntent();
        uid = intent.getIntExtra("id",1)+"";
        mid = intent.getIntExtra("bsid",1)+"";



        isCollect = SPUtils.getInstance().getInt("master_"+uid,-1);

        if (isCollect == -1) {
            title_activity.getRightTextTv().setText("已关注");
        }else {
            title_activity.getRightTextTv().setText("关注");
        }
        //获取标题栏右侧的textview ， 设置点击事件
        title_activity.getRightTextTv().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //修改文本
                if (title_activity.getRightTextTv().getText().toString().equals("已关注")) {
                    ToastUtils.showShort("取消关注");
                    title_activity.getRightTextTv().setText("关注");

                    SPUtils.getInstance().put("master_" + uid, Integer.parseInt(uid));
                }else {
                    ToastUtils.showShort("关注成功");
                    title_activity.getRightTextTv().setText("已关注");

                    SPUtils.getInstance().put("master_"+uid,-1);
                }
            }
        });

        EasyHttp.get("yuejian6/public/index.php/json/recentgame/details")
                .params("game_id",mid)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onSuccess(String data) {

                        Message msg = Message.obtain(); // 实例化消息对象
                        msg.what = 0; // 消息标识
                        msg.obj = data; // 消息内容存放

                        mhandler.sendMessage(msg);

                    }

                    @Override
                    public void onError(ApiException error) {
                        Log.d(TAG, "onFailed: " + error);
                    }
                });

    }

    private void initView() {
        title_activity.getLeftBackTextTv().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityUtils.finishActivity(RecentDetail.class);
            }
        });

    }

    private Handler mhandler = new mHandler();
    private static final String TAG = "ActivityDetail";

    // 步骤1：自定义Handler子类（继承Handler类） & 复写handleMessage（）方法
    class mHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            Log.d(TAG, "handleMessage: " + msg.obj.toString());
            ActivityDetailEntity item = null;
            try {
            item =JSON.parseObject(msg.obj.toString(), ActivityDetailEntity.class);
            }
            catch (Exception e){
                ToastUtils.showShort("获取数据失败");
                return;
            }
            name.setText("名字： "+item.getName());
            address.setText("地址： "+item.getAddress());
            intro.setText("介绍\n"+item.getIntro());
            time.setText("时间：" + item.getTime());

            Glide.with(context)
                    .load(Url.imgUrl+item.getPic())
                    .into(imageView);
        }
    }


}
